﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.UI;
using YJKSparseDistance.ControlLayer;
using YJKSparseDisOfDoor.ModelLayer;
using FlexCell;
using YJKRoomInspection.Utiltiy;
using YArchitech.LIB;
using YJKInspectModelFunc;
using YJKInspectModelFunc.ModelLayer;
using RevitInteractiveUtility.ExternalEventManager;

namespace YJKSparseDisOfDoor
{
	public partial class ResultForm : Form, IModelessFormNoneSwitch
	{
		public Dictionary<string, List<ResultBean>> DicLstAllResult
		{
			get
			{
				return this.m_dicLstAllResult;
			}
		}

		public LocationAWayBean TempLocationAWayBean { get; set; }

		public UIDocument UiDoc { get; set; }

		public IWin32Window m_Handle { get; set; }

		public List<int> NeedTrustData
		{
			get
			{
				return this.m_needTrustData;
			}
		}

		public bool IfContinue
		{
			get
			{
				return this.m_bIfContinue;
			}
		}

		public bool IfBreak
		{
			get
			{
				return this.m_bIfBreak;
			}
		}

		public bool IfManualEsc
		{
			get
			{
				return this.m_bIfManualEsc;
			}
		}

		public ResultForm()
		{
			this.InitializeComponent();
		}

		internal ResultForm(EvacuationDoorDistance evacuationDoorDis, IWin32Window revitHandle, UIDocument inputDoc)
		{
			this.InitializeComponent();
			this.grid_Result.Rows = 1;
			this.grid_Result.Cols = 1;
			this.m_dicLstAllResult = evacuationDoorDis.LstAllResult;
			this.MakeDataTable();
			this.m_Handle = revitHandle;
			this.UiDoc = inputDoc;
		}

		internal ResultForm(EvacuationDoorDistance evacuationDoorDis)
		{
			this.InitializeComponent();
			this.grid_Result.Rows = 1;
			this.grid_Result.Cols = 1;
			this.m_dicLstAllResult = evacuationDoorDis.LstAllResult;
			this.MakeDataTable();
			this.m_useEventManger = ExternalEventManagerSingleton.GetEventManger(true);
			this.m_useEventManger.AppendRequest(new LocationAWayRequest());
			this.m_useEventManger.AppendRequest(new AddValueRequest());
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			this.m_bIfManualEsc = true;
			base.Close();
		}

		private void ResultForm_Load(object sender, EventArgs e)
		{
            //base.Icon = Resource.YArchitechIcon;
		}

		private bool GetResultBeanByRowIndex(int rowIndex, out ResultBean resultBean)
		{
			string text = this.grid_Result.Cell(rowIndex, 7).Text;
			foreach (KeyValuePair<string, List<ResultBean>> keyValuePair in this.m_dicLstAllResult)
			{
				foreach (ResultBean resultBean2 in keyValuePair.Value)
				{
					if (resultBean2.GetHashCode().ToString().Equals(text))
					{
						resultBean = resultBean2;
						return true;
					}
				}
			}
			resultBean = null;
			return false;
		}

		private bool IfStartDoor(int rowIndex)
		{
			bool result;
			try
			{
				string text = this.grid_Result.Cell(rowIndex, 7).Text;
				string text2 = this.grid_Result.Cell(rowIndex + 1, 7).Text;
				if (text.Equals(text2))
				{
					result = true;
				}
				else
				{
					result = false;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		protected override bool ProcessKeyEventArgs(ref Message m)
		{
			int num = 256;
			int num2 = 27;
			if (m.Msg == num && m.WParam.ToInt32() == num2)
			{
				base.Close();
			}
			return base.ProcessKeyEventArgs(ref m);
		}

		private void MakeDataTable()
		{
			this.grid_Result.Cols = 8;
			this.grid_Result.Cell(0, 1).Text = "楼层名称";
			this.grid_Result.Cell(0, 2).Text = "门洞口名称";
			this.grid_Result.Cell(0, 3).Text = "房间名称";
			this.grid_Result.Cell(0, 4).Text = "规范值(M)";
			this.grid_Result.Cell(0, 5).Text = "设计值(M)";
			this.grid_Result.Cell(0, 6).Text = "是否信任";
			this.grid_Result.Cell(0, 7).Text = "HashCode";
			this.grid_Result.Column(0).Visible = false;
			this.grid_Result.Column(7).Visible = false;
			this.grid_Result.Column(1).Locked = false;
			this.grid_Result.Column(2).Locked = false;
			this.grid_Result.Column(3).Locked = false;
			this.grid_Result.Column(4).Locked = false;
			this.grid_Result.Column(5).Locked = false;
			foreach (KeyValuePair<string, List<ResultBean>> keyValuePair in this.m_dicLstAllResult)
			{
				string key = keyValuePair.Key;
				int rows = this.grid_Result.Rows;
				foreach (ResultBean resultBean in keyValuePair.Value)
				{
					if (!this.m_needTrustData.Contains(resultBean.GetHashCode()))
					{
						Grid grid = this.grid_Result;
						int rows2 = grid.Rows;
						grid.Rows = rows2 + 1;
						this.grid_Result.Cell(this.grid_Result.Rows - 1, 1).Text = key;
						this.grid_Result.Cell(this.grid_Result.Rows - 1, 2).Text = resultBean.StartDoor.FamilySymboleName;
						this.grid_Result.Cell(this.grid_Result.Rows - 1, 3).Text = resultBean.RoomName;
						this.grid_Result.Cell(this.grid_Result.Rows - 1, 4).Text = "5";
						this.grid_Result.Cell(this.grid_Result.Rows - 1, 5).Text = Math.Round(resultBean.Distance * 304.8 / 1000.0, 2).ToString();
						this.grid_Result.Cell(this.grid_Result.Rows - 1, 6).CellType = CellTypeEnum.CheckBox;
						this.grid_Result.Cell(this.grid_Result.Rows - 1, 7).Text = resultBean.GetHashCode().ToString();
						int firstRow = this.grid_Result.Rows - 1;
						Grid grid2 = this.grid_Result;
						rows2 = grid2.Rows;
						grid2.Rows = rows2 + 1;
						this.grid_Result.Cell(this.grid_Result.Rows - 1, 1).Text = key;
						this.grid_Result.Cell(this.grid_Result.Rows - 1, 2).Text = resultBean.EndDoor.FamilySymboleName;
						this.grid_Result.Cell(this.grid_Result.Rows - 1, 3).Text = resultBean.RoomName;
						this.grid_Result.Cell(this.grid_Result.Rows - 1, 4).Text = "5";
						this.grid_Result.Cell(this.grid_Result.Rows - 1, 5).Text = Math.Round(resultBean.Distance * 304.8 / 1000.0, 2).ToString();
						this.grid_Result.Cell(this.grid_Result.Rows - 1, 6).CellType = CellTypeEnum.CheckBox;
						this.grid_Result.Cell(this.grid_Result.Rows - 1, 7).Text = resultBean.GetHashCode().ToString();
						int lastRow = this.grid_Result.Rows - 1;
						this.grid_Result.Range(firstRow, 3, lastRow, 3).Merge();
						this.grid_Result.Range(firstRow, 4, lastRow, 4).Merge();
						this.grid_Result.Range(firstRow, 5, lastRow, 5).Merge();
						this.grid_Result.Range(firstRow, 6, lastRow, 6).Merge();
					}
				}
				int num = this.grid_Result.Rows - 1;
				if (rows < num)
				{
					this.grid_Result.Range(rows, 1, num, 1).Merge();
				}
			}
			this.grid_Result.Column(1).Locked = true;
			this.grid_Result.Column(2).Locked = true;
			this.grid_Result.Column(3).Locked = true;
			this.grid_Result.Column(4).Locked = true;
			this.grid_Result.Column(5).Locked = true;
			this.grid_Result.AutoSizeMode = AutoSizeMode.GrowAndShrink;
		}

		private void grid_Result_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			this.m_bIfContinue = true;
			this.m_bIfBreak = false;
			this.m_bIfManualEsc = false;
			this.TempLocationAWayBean = null;
			Cell cell = this.grid_Result.HitTest(e.X, e.Y);
			if (cell != null && cell.Row >= 0 && cell.Col == this.doorNameColIndex)
			{
				if (this.IfStartDoor(cell.Row))
				{
					ResultBean useBean;
					if (!this.GetResultBeanByRowIndex(cell.Row, out useBean))
					{
						return;
					}
					this.TempLocationAWayBean = new LocationAWayBean
					{
						UseBean = useBean,
						UseUIDoc = FrontRoomArch.UseUIApp.ActiveUIDocument,
						UseForm = this,
						IfStartDoor = true
					};
				}
				else
				{
					ResultBean useBean;
					if (!this.GetResultBeanByRowIndex(cell.Row, out useBean))
					{
						return;
					}
					this.TempLocationAWayBean = new LocationAWayBean
					{
						UseBean = useBean,
						UseUIDoc = FrontRoomArch.UseUIApp.ActiveUIDocument,
						UseForm = this,
						IfStartDoor = false
					};
				}
			}
			this.SendEscMessageToRevitAndClearSelect();
		}

		private void btnTrust_Click(object sender, EventArgs e)
		{
			this.m_bIfManualEsc = false;
			base.Close();
		}

		private void grid_Result_CellChange(object Sender, Grid.CellChangeEventArgs e)
		{
			if (e.Col == 6 && e.Row != 0)
			{
				if (this.grid_Result.Cell(e.Row, e.Col).BooleanValue)
				{
					this.m_needTrustData.Add(int.Parse(this.grid_Result.Cell(e.Row, 7).Text));
					return;
				}
				this.m_needTrustData.Remove(int.Parse(this.grid_Result.Cell(e.Row, 7).Text));
			}
		}

		public void ShowForm(IWin32Window parentForm = null)
		{
			if (StaticRescource.m_hWndRevit != null)
			{
				base.Show(StaticRescource.m_hWndRevit);
				return;
			}
			base.Show(StaticRescource.m_hWndRevit);
		}

		public void CloseForm()
		{
			if (!base.IsDisposed)
			{
				base.Close();
			}
		}

		public void ResetContinueBreak()
		{
			this.m_bIfContinue = false;
			this.m_bIfBreak = true;
			this.m_bIfManualEsc = true;
		}

		public void SendEscMessageToRevitAndClearSelect()
		{
			RevitVersionFuncs.ClearSelection(FrontRoomArch.UseUIApp.ActiveUIDocument.Selection);
			if (StaticRescource.m_hWndRevit != null)
			{
				YJKInspectModelFunc.Utility.SendEscToHandle(StaticRescource.m_hWndRevit);
			}
		}

		private Dictionary<string, List<ResultBean>> m_dicLstAllResult = new Dictionary<string, List<ResultBean>>();

		private List<int> m_needTrustData = new List<int>();

		private bool m_bIfManualEsc;

		private bool m_bIfContinue;

		private ExternalEventManagerSingleton m_useEventManger;

		private List<int> m_lstElementIdsNeedDelete = new List<int>();

		private int doorNameColIndex = 2;
	}
}
